/*********************************************************************************
 *FileName:  lin_eeprom.c
 *Author:    ChenJiehong
 *Version:   V1.0
 *Date:      2024-03-19
 ****************************************Includes***********************************/
#include "lin_eeprom.h"

/***************************************Variables***********************************/

/***************************************Functions***********************************/

void EEPROM_StoreLINconfig(uint8_t *config, uint8_t length)
{
	uint8_t data[8] = {0xA5, 0x5A};
	/* 暂时支持5个帧ID */
	if (length > 6)
	{
		return;
	}

	for (uint8_t i = 0; i < length; i++)
	{
		data[i + 2] = config[i];
	}
	__disable_irq();

	EEPROM_Unlock();
	EEPROM_ErasePage(EEPROM_LIN_CFG_PAGE);
	EEPROM_ProgramPage(EEPROM_LIN_CFG_PAGE, (uint32_t *)&data);

	__enable_irq();
}

bool EEPROM_ReadLINconfig(uint8_t *config, uint8_t length)
{
	bool ret = false;
	uint16_t lin_cfg_head = *((uint16_t *)(EEPROMMEM_BASE + 8 * EEPROM_LIN_CFG_PAGE));

	if (lin_cfg_head == 0x5AA5)
	{
		uint8_t *buf = ((uint8_t *)(EEPROMMEM_BASE + 8 * EEPROM_LIN_CFG_PAGE + 2));

		for (uint8_t i = 0; i < length; i++)
		{
			config[i] = buf[i];
		}
		ret = true;
	}
	else
	{
		ret = false;
	}
	return ret;
}

/***************************************END OF FILE*********************************/
